package com.vm.osm.business.controller;

import com.alibaba.druid.util.StringUtils;
import com.google.common.reflect.TypeToken;
import com.vm.osm.business.common.Constants;
import com.vm.osm.business.common.ControllerExceptionHandler;
import com.vm.osm.business.common.exception.BusinessException;
import com.vm.osm.business.dto.eam.equ_part.SearchEamEquPartReq;
import com.vm.osm.business.dto.eam.equ_part.SearchEamEquPartRsp;
import com.vm.osm.business.dto.omp.industry.OmpIndustry;
import com.vm.osm.business.dto.omp.kpi.RspInsp;
import com.vm.osm.business.dto.omp.kpi.RspInspPage;
import com.vm.osm.business.dto.req.checkPlan.ReqAddKpi;
import com.vm.osm.business.dto.req.checkPlan.ReqUpdateKpi;
import com.vm.osm.business.dto.rsp.HttpRspResult;
import com.vm.osm.business.dto.rsp.checkPlan.KpiItem;
import com.vm.osm.business.dto.rsp.checkPlan.RspEquTypePage;
import com.vm.osm.business.dto.rsp.checkPlan.RspKpiPage;
import com.vm.osm.business.entity.OsmKpi;
import com.vm.osm.business.service.IKpiService;
import com.vm.osm.business.util.ApiCallUtil;
import com.vm.osm.business.util.GsonUtil;
import com.vm.osm.business.util.UserDataUtil;
import com.vma.eams.spring.boot.annotation.LogFunc;
import com.vma.eams.spring.boot.annotation.LogModule;
import com.vma.eams.spring.boot.log.LogCollector;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by chenzui on 2018/2/6.
 */
@RestController
@RequestMapping("/v1.0")
@LogModule(type = "kpi",name = "指标管理")
@Api(value = "OsmKpiController",description = "巡检指标",tags = {"OsmKpiController"})
public class OsmKpiController extends ControllerExceptionHandler {
    private Logger LOG = Logger.getLogger(this.getClass());

    @Value("${eam_api_url}")
    private String eamApiUrl;
    @Value("${omp_api_url}")
    private String ompApiUrl;

    @Autowired
    private IKpiService kpiService;

    @Autowired
    private RestTemplate restTemplate;
    /**
     * 新增巡检单
     * @return
     */
    @LogFunc(type = "create",name = "新增巡检指标")
    @ApiOperation(value = "新增巡检指标",notes = "新增巡检指标")
    @ApiImplicitParam(name = "reqAddKpi",value = "新增巡检指标参数",required = true,dataType = "ReqAddKpi")
    @RequestMapping(value = "/add_kpi",method = RequestMethod.POST)
    public void save(@RequestBody ReqAddKpi reqAddKpi,HttpServletRequest request){
        LOG.info("新增巡检指标");
        String enterpriseId = UserDataUtil.getUserSelectedEnterpriseId(request);

        kpiService.addKpiInfo(reqAddKpi,enterpriseId);


    }

    /**
     * 修改巡检指标
     * @param reqUpdateKpi
     * @return
     */
    @LogFunc(type = "update",name = "编辑巡检指标")
    @ApiOperation(value = "修改巡检指标",notes = "修改巡检指标")
    @ApiImplicitParam(name = "reqUpdateKpi",value = "修改巡检指标请求参数",required = true,dataType = "ReqUpdateKpi")
    @RequestMapping(value = "/update_kpi",method = RequestMethod.PUT)
    public void updateEquTypes(@RequestBody ReqUpdateKpi reqUpdateKpi,HttpServletRequest request){
        LOG.info("修改巡检指标");

        kpiService.updateKpi(reqUpdateKpi,request);

        LogCollector.INSTANCE.put("指标名称",reqUpdateKpi.getKpi_name());

    }
    /**
     * 删除设备类型信息
     * @param ids
     * @return
     */
    @LogFunc(type = "delete",name = "删除巡检指标")
    @ApiOperation(value = "删除巡检指标",notes = "删除巡检指标")
    @RequestMapping(value = "/del_kpi/{id}",method = RequestMethod.DELETE)
    public void deleteEquType(@ApiParam(required=true, name="id", value="系统编号,支持多列删除,用英文逗号隔开") @PathVariable("id") String ids){
        LOG.info("删除设备类型信息");

        String[] idArr = ids.split(",");
        for(int i=0;i<idArr.length;i++){

            kpiService.delete(idArr[i]);
//            LogCollector.INSTANCE.put("指标名称",name);

        }

    }

    @ApiOperation(value = "获取设备类型",notes = "获取设备类型")
    @RequestMapping(value = "/get_equ_types",method = RequestMethod.GET)
    public Object getEquTypes(HttpServletRequest request){
        //企业编号
        String enterpriseId = UserDataUtil.getUserSelectedEnterpriseId(request);

        LOG.info("enterpriseId"+enterpriseId);
        // TODO: 2018/2/22  根据企业编号调用设备类型接口
        String getUrl = eamApiUrl+"/eam/v1.0/"+enterpriseId+"/equ_type_trees";

        return restTemplate.getForObject(getUrl,Object.class);

    }

       /**
     * 根据企业ID与设备类别ID。查询kpi指标
     */
    @ApiOperation(value = "查询kpi指标",notes = "根据企业ID与设备类别ID")
    @RequestMapping(value = "/get_kpis",method = RequestMethod.GET)
    public List<KpiItem> getKpiByDeviceId(
            @ApiParam(name = "equ_type_id",value = "设备类型ID",required = false) @RequestParam(value = "equ_type_id",required = false) String equTypeId,
            @ApiParam(name = "kpi_name",value = "指标名称") @RequestParam(value = "kpi_name",required = false) String kpiName,
            HttpServletRequest request){
        String enterpriseId = UserDataUtil.getUserSelectedEnterpriseId(request);

        return kpiService.getKpiByCond(equTypeId,enterpriseId,kpiName);
    }


    /**
     * 根据企业ID与设备类别ID。查询kpi指标
     */
    @ApiOperation(value = "查询kpi指标分页",notes = "根据企业ID与设备类别ID")
    @RequestMapping(value = "/kpi/page",method = RequestMethod.GET)
    public RspKpiPage getKpiPage(
            @ApiParam(name = "equ_type_id",value = "设备类型ID",required = false) @RequestParam(value = "equ_type_id",required = false) String equTypeId,
            @ApiParam(name = "kpi_name",value = "指标名称") @RequestParam(value = "kpi_name",required = false) String kpiName,
            @ApiParam(required=true, name="page_num", value="分页数") @RequestParam("page_num") Integer pageNum,
            @ApiParam(required=true, name="page_size", value="分页大小") @RequestParam("page_size") Integer pageSize,
            HttpServletRequest request){
        String enterpriseId = UserDataUtil.getUserSelectedEnterpriseId(request);

        return kpiService.getPage(equTypeId,enterpriseId,kpiName,pageNum,pageSize);
    }


    @ApiOperation(value = "创建企业时通知运维同步kpi",notes = "创建企业时通知运维同步kpi")
    @RequestMapping(value = "/kpi/synchronous/{enterpriseId}",method = RequestMethod.GET)
    public void synchronous(@ApiParam(required=true, name="enterpriseId", value="企业ID") @PathVariable("enterpriseId") String enterpriseId){

        LOG.info("创建企业时通知运维同步kpi");



         String url = ompApiUrl+"/omp/v1.0/inspections/all/inspectionInfo";
         ResponseEntity<RspInsp[]> entity = restTemplate.getForEntity(url, RspInsp[].class);
         List<RspInsp> list = Arrays.asList(entity.getBody());
         for(RspInsp insp : list){
                ReqAddKpi reqAddKpi = new ReqAddKpi();
                String content = insp.getContent();
                if(!StringUtils.isEmpty(content)){
                    List<String> values = new ArrayList();
                    String[] valuesArr = content.split("|");
                    for(int i=0;i<valuesArr.length;i++){
                        values.add(valuesArr[i]);
                    }
                    reqAddKpi.setItems(values);
                }
                reqAddKpi.setDevice_type(insp.getDevice_type_id());
                reqAddKpi.setKpi_name(insp.getName());
                reqAddKpi.setSource_type(insp.getType());
                kpiService.addKpiInfo(reqAddKpi,enterpriseId);

        }



    }
}
